package com.lychee.voicetranscribe;

import android.media.MediaRecorder;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.File;
import java.io.IOException;

/**
 * Created by Lychee of on 2018/3/9 14:49.
 * <p>
 * Author: HeJingzhou
 * <p>
 * Email: tcowork@163.com
 */
public class AudioRevicer {
    private OnAudioStatusListener onAudioStatusListener;
    private MediaRecorder mMediaRecorder;
    private String savePath;
    private final int MAX_RECORD_TIME = 1000 * 60 * 2;
    private long startTime;
    private String autoFilePath;
    private String TAG = getClass().getSimpleName();
    private Handler handler = new Handler();
    private UpdateRun updateRun = new UpdateRun();


    public AudioRevicer() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            savePath = Environment.getExternalStorageDirectory().getAbsolutePath();
        }
    }

    public AudioRevicer(String savePath) {
        mMediaRecorder = new MediaRecorder();
        File file = new File(savePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        this.savePath = savePath;
    }

    /**
     * 开始录音
     */
    public void startRecord() {
        if (mMediaRecorder == null) {
            mMediaRecorder = new MediaRecorder();
        }
        //设置声源取自麦克风
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        //设置音频文件的编码格式
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        //设置音频文件的格式
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        autoFilePath = savePath + File.separator + "AAAAAAATEST" + ".amr";
        if (new File(autoFilePath).exists()) {
            new File(autoFilePath).delete();
        }
        mMediaRecorder.setOutputFile(autoFilePath);
        mMediaRecorder.setMaxDuration(MAX_RECORD_TIME);
        try {
            mMediaRecorder.prepare();
            mMediaRecorder.start();
            startTime = System.currentTimeMillis();
            updateCurrAudioData();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 刷新当前音频
     */
    private void updateCurrAudioData() {
        if (mMediaRecorder != null) {
            final int BASE = 1;
            //获取最大振幅
            double ratio = (double) mMediaRecorder.getMaxAmplitude() / BASE;
            // 分贝
            double db = 0;
            if (ratio > 1) {
                db = 20 * Math.log10(ratio);
            }

            if (onAudioStatusListener != null) {
                onAudioStatusListener.currStatus(new AudioData(ratio, db, startTime));
            }
            handler.postDelayed(updateRun, 100);
        }
    }


    private class UpdateRun implements Runnable {
        @Override
        public void run() {
            updateCurrAudioData();
        }
    }

    /**
     * 取消录音
     */
    public void cancelRecord() {
        if (mMediaRecorder != null) {
            mMediaRecorder.stop();
            mMediaRecorder.reset();
            //释放资源与这个MediaRecorder对象相关联。
            mMediaRecorder.release();
            mMediaRecorder = null;
        }
        Log.i(TAG, "cancelRecord: " + autoFilePath);
    }

    public String getSavePath() {
        return savePath;
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

    public interface OnAudioStatusListener {
        void currStatus(AudioData audioData);
    }

    public void setOnAudioStatusListener(OnAudioStatusListener onAudioStatusListener) {
        this.onAudioStatusListener = onAudioStatusListener;
    }
}
